<script>
import pagination from '@crud/PaginationCustom.vue'
import CRUD, { presenter, header, form, crud } from '@/components/Crud/customCrud'
import { requestGet } from '@/api/system/common'
import DbTemplateSetting from '@/views/notice/temp/setting/qyapi/index.vue'
import DingTalkTemplateSetting from '@/views/notice/temp/setting/dingTalk/index.vue'
import EmailTemplateSetting from '@/views/notice/temp/setting/email/index.vue'
import MobileTemplateSetting from '@/views/notice/temp/setting/mobile/index.vue'
import NoticeTemplateSetting from '@/views/notice/temp/setting/notice/index.vue'
import WeiCatTemplateSetting from '@/views/notice/temp/setting/WCat/index.vue'
const defaultForm = {
  id: null,
  name: '', // 模版名称
  code: '', // 模版编码
  content: '', // 消息内容
  params: '', // 模版参数
  mblx: '', // 消息类型
  channel: null, // 所属通道
  notice: false, // 是否推送站内信
  state: true // 是否启用
}
export default {
  name: 'TemplateManager',
  components: { pagination, DbTemplateSetting, DingTalkTemplateSetting, EmailTemplateSetting, MobileTemplateSetting,
    NoticeTemplateSetting, WeiCatTemplateSetting
  },
  cruds() {
    return CRUD({
      title: '消息模版',
      url: 'rest/api/sys/notice/v1/temp',
      props: {
        searchToggle: true
      }
    })
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  data() {
    return {
      channelList: [],
      noticeList: []
    }
  },
  created() {
    this.initAllChannel()
    this.initAllNoticeType()
  },
  methods: {
    // 获取全部通道信息
    initAllChannel() {
      requestGet('rest/api/sys/notice/v1/channel/blurry', { state: true })
        .then(res => {
          const { data } = res
          this.channelList = data.data
        }).catch(() => {
          this.channelList = []
        })
    },
    // 获取全部消息类型
    initAllNoticeType() {
      requestGet('rest/api/constant/notice/v1/channel/type', null)
        .then(res => {
          const { data } = res
          this.noticeList = data.data
        }).catch(() => {
          this.noticeList = []
        })
    },
    // 获取通道名称
    getChannel(data) {
      let name = {}
      if (this.channelList.length > 0) {
        this.channelList.forEach(item => {
          if (item.id === data.channel) {
            name = item
          }
        })
      }
      return name
    },
    getChannelName(data) {
      return this.getChannel(data).name
    },
    // 点击模版设置按钮
    setting(data) {
      const channel = this.getChannel(data)
      this.$nextTick(() => {
        this.$refs[channel.type].openDialog({ form: data, channel: channel })
      })
    }
  }
}
</script>
<template>
  <div class="app-container">
    <el-card class="box-card">
      <div
        v-for="(item,index) in crud.data"
        :key="'data-'+index"
        class="box-ul-li"
      >
        <div class="box-ul-header">
          {{ item.name }}
          <div v-if="item.state" class="header-state start">
            启用
          </div>
          <div v-else class="header-state disabled">
            禁用
          </div>
        </div>
        <div class="box-ul-line">
          编码：{{ item.code }} {{ '('+getChannelName(item)+')' }}
        </div>
        <div class="box-ul-footer">
          <div class="box-ul-btn" @click="crud.toEdit(item)">编辑</div>
          <div class="box-ul-btn" @click="crud.doDelete(item)">删除</div>
          <div class="box-ul-btn" @click="setting(item)">设置</div>
        </div>
      </div>
      <div class="box-ul-li add">
        <div class="box-ul-content" @click="crud.toAdd">
          <div class="box-ul-icon">
            <i class="el-icon-plus" />
          </div>
          <div class="box-ul-title">
            添加模版
          </div>
        </div>
      </div>
      <!--分页组件-->
      <div class="box-pager">
        <pagination />
      </div>
    </el-card>
    <!--表单渲染-->
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crud.status.cu > 0"
      :title="crud.status.title"
      width="700px"
    >
      <el-form
        ref="form"
        :inline="true"
        :model="form"
        size="small"
        label-width="120px"
      >
        <el-form-item
          :rules="[{ required: true, message: '请输入模版名称', trigger: 'blur' }]"
          label="模版名称"
          prop="name"
        >
          <el-input v-model="form.name" placeholder="请输入模版名称" style="width: 400px;" />
        </el-form-item>
        <el-form-item
          :rules="[{ required: true, message: '请输入模版名称', trigger: 'blur' }]"
          label="模版编码"
          prop="code"
        >
          <el-input v-model="form.code" placeholder="请输入模版名称" style="width: 400px;" />
        </el-form-item>
        <el-form-item
          :rules="[{ required: true, message: '请选择通道', trigger: 'changer' }]"
          label="通道"
          prop="channel"
        >
          <el-select
            v-model="form.channel"
            clearable
            placeholder="请选择通道"
            style="width: 400px;"
          >
            <el-option
              v-for="(item,index) in channelList"
              :key="'type-'+index"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          :rules="[{ required: true, message: '请选择消息类型', trigger: 'changer' }]"
          label="消息类型"
          prop="mblx"
        >
          <el-select
            v-model="form.mblx"
            clearable
            placeholder="请选择消息类型"
            style="width: 400px;"
          >
            <el-option
              v-for="(item,index) in noticeList"
              :key="'type-'+index"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="是否推送站内信"
          prop="notice"
          :rules="[{ required: true, message: '请选择是否推送站内信', trigger: 'changer' }]"
        >
          <el-radio-group
            v-model="form.notice"
          >
            <el-radio
              v-for="item in enable"
              :key="item.id"
              :label="item.value"
            >{{ item.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group
            v-model="form.state"
          >
            <el-radio
              v-for="item in enable"
              :key="item.id"
              :label="item.value"
            >{{ item.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU">取消</el-button>
        <el-button
          :loading="crud.status.cu === 2"
          type="primary"
          @click="crud.submitCU"
        >确认</el-button>
      </div>
    </el-dialog>
    <db-template-setting ref="qyapi" />
    <ding-talk-template-setting ref="dingTalk" />
    <email-template-setting ref="email" />
    <mobile-template-setting ref="mobile" />
    <notice-template-setting ref="notice" />
    <wei-cat-template-setting ref="WCat" />
  </div>
</template>
<style scoped>
.el-card ::v-deep .el-card__body{
  width: 100%;
  display: flex;
  flex-flow: row wrap;
}
.box-card {
  min-height: 100vh;
  width: 100%;
}
.box-ul-li {
  width: 275px;
  height: 110px;
  border-radius: 4px;
  border: 1px solid #DCDFE6;
  color: #99a9bf;
  cursor: pointer;
  margin-left: 10px;
  margin-top: 10px;
  padding: 0 10px;
  box-sizing: border-box;
}
.box-ul-li.add {
  border: 1px dashed #DCDFE6;
  text-align: center;
}
.box-ul-li.add:hover {
  border: 1px dashed #5cb6ff;
  color: #5cb6ff ;
}
.box-ul-content {
  width: 100%;
  height: 60px;
  margin-top: 20px;
}
.box-ul-icon {
  width: 100%;
  height: 40px;
  line-height: 40px;
  font-size: 32px;
}
.box-ul-title {
  width: 100%;
  height: 20px;
  line-height: 20px;
  font-size: 14px;
}
.box-ul-header {
  height: 40px;
  line-height: 40px;
  text-align: left;
  font-size: 14px;
  color: #2c3e50;
  font-weight: bold;
  position: relative;
}
.box-ul-line {
  width: 100%;
  height: 40px;
  line-height: 40px;
  text-align: left;
  color: #909399;
  font-size: 14px;
}
.box-ul-footer {
  width: 100%;
  height: 30px;
  line-height: 30px;
  display: flex;
}
.box-ul-btn {
  width: 40px;
  height: 30px;
  position: relative;
  text-align: center;
  line-height: 30px;
  font-size: 13px;
  color: #409eff;
}
.box-ul-btn:not(:first-child):before {
  content: '';
  position: absolute;
  width: 2px;
  height: 14px;
  background-color: #909399;
  top: 8px;
  left: -1px;
}
.header-state {
  position: absolute;
  width: 60px;
  height: 30px;
  font-size: 14px;
  top: 5px;
  text-align: center;
  line-height: 30px;
  right: -10px;
}
.header-state.start {
  color: #67C23A;
}
.header-state.disabled {
  color: #F56C6C;
}
.start:after {
  content: '';
  position: absolute;
  width: 10px;
  height: 10px;
  border-radius: 5px;
  top: 10px;
  right: 48px;
  background-color: #67C23A;
}
.disabled:after {
  content: '';
  position: absolute;
  width: 10px;
  height: 10px;
  border-radius: 5px;
  top: 10px;
  right: 48px;
  background-color: #F56C6C;
}
.box-pager {
  width: 100%;
  height: 50px;
  margin-top: 20px;
}
</style>
